package cn.springboot.erp.controller;

import cn.springboot.erp.annotation.Log;
import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Permission;
import cn.springboot.erp.enums.Operation;
import cn.springboot.erp.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("permission")
@Api(tags = "权限管理")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @Log(description = Operation.SELECT,model = "权限管理")
    @ApiOperation("数据分页")
    @PostMapping("page")
    @RequiresPermissions("sys:permission:select")
    public ResponseResult listPage(String title){
        return permissionService.listPage(title);
    }

    @Log(description = Operation.ADD,model = "权限管理")
    @ApiOperation("添加权限")
    @PostMapping("insertOrUpdate")
    @RequiresPermissions(value = {"sys:permission:add","sys:permission:update"})
    public ResponseResult insertOrUpdate(@RequestBody Permission permission){
        System.out.println("permission = " + permission);
        permissionService.saveOrUpdate(permission);
        return ResponseResult.Success();
    }

    @Log(description = Operation.SELECT,model = "权限管理")
    @ApiOperation("查询指定权限")
    @GetMapping("selectById/{id}")
    @RequiresPermissions("sys:permission:select")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        //根据id查询指定权限
        // 获取权限对象
        Permission permission = permissionService.getById(id);

        // 获取pid
        Integer pid = permission.getPid();
        String title = "";
        if (pid!=0){ // 不是目录权限对象
            // 获取标题
            title = permissionService.getById(permission.getPid()).getTitle();
        }
        return ResponseResult.Success().data("permission",permission).data("title",title);
    }

    @Log(description = Operation.DELETE,model = "权限管理")
    @ApiOperation("删除权限")
    @GetMapping("deleteById/{id}")
    @RequiresPermissions("sys:permission:delete")
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        return permissionService.deletePermission(id);
    }

    @Log(description = Operation.SELECT,model = "权限管理")
    @ApiOperation("权限树形结构")
    @GetMapping("selectPermission")
    @RequiresPermissions("sys:permission:select")
    public ResponseResult selectPermission(){
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.select(Permission::getId,Permission::getTitle,Permission::getPid);
        List<Permission> list = permissionService.list(lambda);
        return ResponseResult.Success().data("tree",list);
    }
}
